package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author success
 * @date 2024-12-30
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {

        NursingPlanVo nursingPlanVo = new NursingPlanVo();

        //1.根据id查询护理计划
        NursingPlan nursingPlan = getById(id);
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        //2.根据护理计划的id获取关联的护理项目信息
        List<NursingProjectPlanVo> nursingProjectPlanVo = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(nursingProjectPlanVo);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {

        //1.保存基本信息
        NursingPlan nursingPlan = new NursingPlan();
        //属性拷贝
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        save(nursingPlan);

        //2.保存护理项目的信息
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        int count = nursingProjectPlanMapper.batchInsert(projectPlans, nursingPlan.getId());

        return count > 0 ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {

        //1.修改护理计划所关联的护理项目
        if (nursingPlanDto.getProjectPlans() != null && !nursingPlanDto.getProjectPlans().isEmpty()) {
            //1.1 删除关联的护理项目
            nursingProjectPlanMapper.deleteByPlanId(nursingPlanDto.getId());
            //1.2 添加关联的护理项目
            nursingProjectPlanMapper.batchInsert(nursingPlanDto.getProjectPlans(), nursingPlanDto.getId());
        }

        //2.修改基本信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);

        return updateById(nursingPlan) ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {

        //删除关联的护理项目
        nursingProjectPlanMapper.deleteByPlanIds(ids);

        return removeBatchByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanById(Long id) {

        //删除关联的护理项目
        nursingProjectPlanMapper.deleteByPlanId(id);

        return removeById(id) ? 1 : 0;
    }
}
